package com.nytimes.ohos.store.sample.util;


import com.nytimes.ohos.store.sample.data.model.Image;
import com.squareup.picasso.Transformation;
import ohos.media.image.PixelMap;


public class BitmapTransform implements Transformation {
    public int targetWidth;
    public int targetHeight;

    public BitmapTransform(int maxWidth, int maxHeight, Image image) {


        double aspectRatio;
        if (image.width() >= image.height()) {
            targetWidth = maxWidth;
            aspectRatio = (double) image.height() / (double) image.width();
            targetHeight = (int) (targetWidth * aspectRatio);
        } else {
            targetHeight = maxHeight;
            aspectRatio = (double) image.width() / (double) image.height();
            targetWidth = (int) (targetHeight * aspectRatio);
        }
    }


    @Override
    public PixelMap transform(PixelMap source) {
        PixelMap.InitializationOptions initializationOptions = new PixelMap.InitializationOptions();
        initializationOptions.size.width=targetWidth;
        initializationOptions.size.height=targetHeight;
        PixelMap pixelMap = PixelMap.create(source, initializationOptions);
        if (pixelMap != source) {
            source.release();
        }
        return pixelMap;
    }

    @Override
    public String key() {
        return "";
    }
}
